package com.hucheng.provider.user.controller;

import com.hucheng.provider.user.dao.UserRepository;
import com.hucheng.provider.user.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;

/**
 * @Author: 胡成
 * @Version: 0.0.1V
 * @Date: 2019-05-27
 * @Description: User Controller
 **/
@RestController
public class UserController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserRepository userRepository;


    @GetMapping("{id}")
    public User findById(@PathVariable Long id) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof UserDetails) {
            UserDetails userDetails = (UserDetails) principal;
            Collection<? extends GrantedAuthority> collection = userDetails.getAuthorities();
            for (GrantedAuthority authority : collection) {
                LOGGER.info("当前用户是{},角色是{}",userDetails.getUsername(),authority.getAuthority());
            }
        }else{
            LOGGER.info("do other things");
        }
        return userRepository.findOne(id);
    }

//    @GetMapping("user-user/{id}")
//    public User findByIdUser(@PathVariable Long id){
//        return
//    }
}
